package 极客时间.concurrence.并发编程理论基础.java线程;

/**
 * @Author idea
 * @Date created in 8:32 上午 2020/7/21
 */
public class Demo1 {

    public Object obj;

    public void demo1(){
        Runnable blockRunnable = new Runnable() {
            @Override
            public void run() {
                synchronized (obj){
                    System.out.println("从runnable 进入到了 block状态");
                }
            }
        };
        Thread t1 = new Thread(blockRunnable);
        t1.start();
    }

    public void demo2(){
        Runnable timeWaitRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread timeWait = new Thread(timeWaitRunnable);
        timeWait.start();
    }

    public void demo3() throws InterruptedException {
        Runnable waiting = new Runnable() {
            @Override
            public void run() {
                System.out.println("进入到了waiting状态");
            }
        };
        Thread thread = new Thread(waiting);
        thread.start();
        thread.join();
    }


    public void demo4(){
        //线程此时处于一个new的状态 cpu并没有真正分配资源
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("this is thread");
            }
        });
        //此时thread已经进入到了run的状态，cpu有分配资源了
        thread.start();
    }

    public void demo5(){
        //线程此时处于一个new的状态 cpu并没有真正分配资源
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("this is thread");
            }
        });
        //此时thread已经进入到了run的状态，cpu有分配资源了
        thread.start();
        //从run进入到interrupt状态
        thread.interrupt();
        //不推荐使用stop这个函数，因为此时并没有释放掉锁
        thread.stop();
    }

    public static void main(String[] args) {

    }
}
